/*
 * AnalyseWall.java
 *
 * Created on January 29, 2007, 1:20 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package net.sf.iSnake.aimodule;

import java.util.Vector;
import net.sf.iSnake.core.GameAreaCoordinate;

/**
 *
 * @author Administrator
 */
public class AnalyseWall {
    final int UP=0, DOWN=1, RIGHT=2, LEFT=3;
    final int UP_RIGHT=4,UP_LEFT=5,DOWN_RIGHT=6,DOWN_LEFT=7,HORIZONTAL=8,VERTICAL=9;

    Vector wall;
    
    GameAreaCoordinate position[]=new GameAreaCoordinate[2];
    short x[]=new short[2];
    short y[]=new short[2];
    int endPoint[][]=new int[3][3];
    int dir,dir1,dir2,corner,len;
    
    public AnalyseWall(Vector wall) {
        this.wall=wall;
        len=wall.size();
        dir=dir1=dir2=-1;
        endPoint1();
        endPoint2();
        System.out.println(wall.size());
    }
    public void endPoint1(){      //verify that wall doesn't get modified on x[],y[] being modified.
        x[0]=((GameAreaCoordinate)((wall.elementAt(0)))).getX();
        y[0]=((GameAreaCoordinate)((wall.elementAt(0)))).getY();
        x[1]=((GameAreaCoordinate)((wall.elementAt(1)))).getX();
        y[1]=((GameAreaCoordinate)((wall.elementAt(1)))).getY();
        switch(y[0]-y[1]){
            case 0:     
                dir=HORIZONTAL;
                endPoint[0][0]=-1;
                endPoint[0][1]=-1;
                endPoint[0][2]=-1;
                endPoint[1][1]=y[0];
                endPoint[2][1]=y[0];
                x[1]=((GameAreaCoordinate)((wall.elementAt(len-1)))).getX();
                switch(x[0]-x[1]){
                    case 1:
                        endPoint[1][0]=x[0]+1;
                        endPoint[1][2]=LEFT;
                        endPoint[2][0]=x[1]-1;
                        endPoint[2][2]=RIGHT;
                        break;
                    case -1:
                        endPoint[1][0]=x[0]-1;
                        endPoint[1][2]=RIGHT;
                        endPoint[2][0]=x[1]+1;
                        endPoint[2][2]=LEFT;
                        break;
                }
                break;
            case 1:
                dir1=UP;
                endPoint[0][0]=x[0];
                endPoint[0][1]=y[0]+1;
                endPoint[0][2]=UP;
                break;
            case -1:
                dir1=DOWN;
                endPoint[0][0]=x[0];
                endPoint[0][1]=y[0]-1;
                endPoint[0][2]=DOWN;
        }
        if(dir!=HORIZONTAL)
            endPoint2();
        
    }
    public void endPoint2(){
        int i;
        for(i=2,corner=2;i<wall.size();i++,corner++){
            x[0]=x[1];
            y[0]=y[1];
            x[1]=((GameAreaCoordinate)((wall.elementAt(i)))).getX();
            y[1]=((GameAreaCoordinate)((wall.elementAt(i)))).getY();
            if(x[1]!=x[0]){
                switch((x[1]-x[0])){
                    case 1:
                        dir2=RIGHT;
                        break;
                    case -1:
                        dir2=LEFT;
                        break;
                }
                y[1]=((GameAreaCoordinate)((wall.elementAt(len-1)))).getY();
                
                if(dir1==UP&&dir2==RIGHT){
                    dir=UP_RIGHT;
                    endPoint[1][0]=x[0]-1;
                    endPoint[1][1]=y[0]-1;
                    endPoint[1][2]=DOWN_RIGHT;
                    endPoint[2][0]=x[1];
                    endPoint[2][1]=y[1]+1;
                    endPoint[2][2]=LEFT;
                }
                if(dir1==UP&&dir2==LEFT){
                    dir=UP_LEFT;
                    endPoint[1][0]=x[0]+1;
                    endPoint[1][1]=y[0]-1;
                    endPoint[1][2]=DOWN_LEFT;
                    endPoint[2][0]=x[1];
                    endPoint[2][1]=y[1]-11;
                    endPoint[2][2]=RIGHT;
                }
                if(dir1==DOWN&&dir2==RIGHT){
                    dir=DOWN_RIGHT;
                    endPoint[1][0]=x[0]-1;
                    endPoint[1][1]=y[0]+1;
                    endPoint[1][2]=UP_RIGHT;
                    endPoint[2][0]=x[1];
                    endPoint[2][1]=y[1]+1;
                    endPoint[2][2]=LEFT;
                }
                if(dir1==DOWN&&dir2==LEFT){
                    dir=DOWN_LEFT;
                    endPoint[1][0]=x[0]+1;
                    endPoint[1][1]=y[0]+1;
                    endPoint[1][2]=UP_LEFT;
                    endPoint[2][0]=x[1];
                    endPoint[2][1]=y[1]-1;
                    endPoint[2][2]=RIGHT;
                }
                break;
            }
        }
        if(dir2!=RIGHT&&dir2!=LEFT){
            dir=VERTICAL;
            endPoint[1][0]=x[1];
            switch(dir1){
                case UP:
                    endPoint[1][1]=y[1]-1;
                    endPoint[1][2]=DOWN;
                    break;
                case DOWN:
                    endPoint[1][1]=y[1]+1;
                    endPoint[1][2]=UP;
                    break;
            }
            endPoint[2][0]=-1;
            endPoint[2][1]=-1;
            endPoint[2][2]=-1;
        }
    }
}
